package com.rzico.sbl.ui.report.manager

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.extend.CustomValueFormatter
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentManagerSecondBinding
import com.rzico.sbl.other.*
import com.rzico.sbl.ui.report.other.ReportUrl
import com.rzico.sbl.viewmodel.ReportManagerViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.NavigationFragment
import org.json.JSONObject

class ManagerSecondFragment : NavigationFragment() {

    private var mBinding: FragmentManagerSecondBinding? = null

    private var mBeginDate = getBeforeDate(6)
    private var mEndDate = getTodayDate()

    override fun getViewModel() = get<ReportManagerViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentManagerSecondBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initListener()

        mBinding?.managerChart?.initChart()
    }

    private fun initListener() {
        mBinding?.apply {
            secondAll.oneClick { startActivityEx<ReportSecondActivity>() }

            secondTime.addTimeListener { start, end, _ ->
                mBeginDate = start
                mEndDate = end

                getViewModel().cancelRequest()
                getData()
            }
        }
    }

    override fun onResume() {
        super.onResume()

        getData()
    }

    /**
     * 取消请求
     */
    override fun onPause() {
        super.onPause()
        getViewModel().cancelRequest()
    }

    /**
     * 设置饼状图数据
     */
    private fun setChartData(
        total: Int,
        amountArray: Array<Int>
    ) {
        mBinding?.let {
            it.managerChart.apply {
                if (total == 0) clear()
                else {
                    formartCenterText(
                        value = total.toString(),
                        hint = "订单总数",
                        valueColor = R.color.colorAccent
                    )

                    setPieData(
                        colors = getViewModel().orderColor(),
                        formatter = CustomValueFormatter(),
                        PieEntry(amountArray[0].toFloat(), "待接单"),
                        PieEntry(amountArray[1].toFloat(), "配送中"),
                        PieEntry(amountArray[2].toFloat(), "已完成")
                    )
                }

                animateY(1000, Easing.EaseInOutQuad)
            }
        }
    }

    /**
     * 获取订单饼状图数据
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .request(
                isLoadingBefore = false,
                isLoadingAfter = false,
                requestUrl = ReportUrl.getBaseUrl().zOrderSum,
                requestMethod = HttpMethod.GET,
                params = arrayOf(
                    "beginDate" to mBeginDate,
                    "endDate" to mEndDate
                ),
                onSuccess = {
                    val obj = JSONObject(it)

                    arrayOf(
                        obj.optNotNull("unconfirmed").intValue,
                        obj.optNotNull("confirmed").intValue,
                        obj.optNotNull("completed").intValue
                    ).apply {
                        setChartData(sum(), this)
                    }
                }
            )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

}